package crypt

import (
	"crypto/hmac"
	"crypto/sha256"
	"encoding/base64"
	"encoding/hex"
)

type Hmac struct {
}

// HMAC-SHA256计算签名并返回hex
func (this *Hmac) Sha256HexSign(plaintext string, secKey string) string {
	h := hmac.New(sha256.New, []byte(secKey))
	h.Write([]byte(plaintext))
	return hex.EncodeToString(h.Sum(nil))
}

// HMAC-SHA256计算签名并返回base64
func (this *Hmac) Sha256B64Sign(plaintext string, secKey string) string {
	h := hmac.New(sha256.New, []byte(secKey))
	h.Write([]byte(plaintext))
	return base64.StdEncoding.EncodeToString(h.Sum(nil))
}

func NewHmac() *Hmac {
	return &Hmac{}
}
